//
//  BN_CommentDisPViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-24.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_CommentDisPViewController.h"

#define DUG 42

@interface BN_CommentDisPViewController () {
    NSArray *netArr_;
    NSString *testStr_;
    BN_MyInfoHelper *myInfoHelper_;
}
@end

@implementation BN_CommentDisPViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"宝宝周评";
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVc)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // UITabelView
    self.commentTableView = [[UITableView alloc]init];
    self.commentTableView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    if ([self.commentTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.commentTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    [self setExtraCellLineHidden:self.commentTableView];
    [self.view addSubview:self.commentTableView];
    
    switch (MAIN_VERSION) {
        case 6:
            self.commentTableView.backgroundColor = [UIColor whiteColor];
            self.commentTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
            break;
        default:
            self.commentTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            self.commentTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
            self.commentTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 0, 0);
            break;
    }
    // 通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(parentLookComment:) name:KMINE_PARENT_LOOK_FULLTEXT object:nil];
    
    // helper
    myInfoHelper_ = [[BN_MyInfoHelper alloc]init];
    [self performSelectorInBackground:@selector(requestNetData) withObject:nil];
    
    // 风火轮
    [self viewLoadHintView];
}

#pragma mark
#pragma mark 网络请求
- (void)requestNetData {
    NSString *childId = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:USERDEFAULTS_BABY_ID]];
    __weak typeof(self) weakSelf = self;
    myInfoHelper_.queryEvalListCompleteHandler = ^(id result,NSString *errMsg){
        if (errMsg) {
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
            [weakSelf cancelHintView];
            
        }else{
            [weakSelf cancelHintView];
            weakSelf.netArr = (NSArray *)result;
            [weakSelf.commentTableView reloadData];
        }
    };
    [myInfoHelper_ refreshEvalList:childId];

}

#pragma mark
#pragma mark UITableView Delegate 

-(void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [tableView setTableFooterView:view];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NurserySchoolCellTableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight-DUG;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.netArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"nurserySchoolCellTableViewCell";
    NurserySchoolCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"NurserySchoolCellTableViewCell" owner:nil options:nil]objectAtIndex:0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    cell.titleLabel.text = @"";
    cell.contLabel.text = [NSString stringWithFormat:@"%@",self.netArr[indexPath.section][@"evalCont"]];
    cell.browseFullTextBtn.hidden = YES;
    cell.lineLabel.hidden = YES;
    [cell toMine_Parent_Group];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    timeLabel.backgroundColor = APP_KINDER_TIME_LABEL_BGCOLOR;
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.layer.masksToBounds = YES;
    [timeLabel.layer setCornerRadius:9];
    timeLabel.font = [UIFont systemFontOfSize:12];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:timeLabel];
    
    // 2014-05-04 18:46:33.0
    NSString *dateStr = _netArr[section][@"evalDate"];
    NSRange rang = NSMakeRange(0, 19);
    NSString *strRang = [dateStr substringWithRange:rang];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateTime = [formatter dateFromString:strRang];
    timeLabel.text = [IMB_TimeFormatModel timeFormat:dateTime];
    [timeLabel sizeToFit];
    timeLabel.frame = CGRectMake((SCREEN_WIDTH-CGRectGetWidth(timeLabel.frame))/2-10, (35-CGRectGetHeight(timeLabel.frame))/2-1.5, CGRectGetWidth(timeLabel.frame)+20, CGRectGetHeight(timeLabel.frame)+3);

    return headView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark
#pragma mark Action 

- (void)backRootVc {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)parentLookComment:(NSNotification *)notification {
    BN_CommentXQViewController *parentLookCommentXQVc = [[BN_CommentXQViewController alloc]init];
    [self.navigationController pushViewController:parentLookCommentXQVc animated:YES];
}

#pragma mark
#pragma mark 资源

- (void)releaseResource {

}

- (void)dealloc
{
    NSLog(@"周评展示列表 释放...");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
